package com.blog.admin.mapper.apiMapper;

import com.blog.admin.vo.BlogArticleVO;
import com.blog.admin.vo.BlogClassifyVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author tangqj
 * @Description 文章模块部分接口
 * @create 2020/3/9 17:08
 */
public interface ArticleMapper {
    /**
     * @param id       文章id
     * @param realname 作者
     * @param title    标题
     * @param catalogs 分类id
     * @return
     */
    @Select("<script> select a.*,sui.realname,(select count(*) from blog_comment where  pk_article = a.id) commentCount from blog_article a\n" +
            "        left join sys_user b on a.pk_user = b.id\n" +
            "        left join sys_user_information sui on a.pk_user = sui.pk_user\n" +
            "        where ifnull(a.status,2)= 2" +
            "            <if test=\"title !=null and title !=''\">\n" +
            "                and a.title like \"%\"#{title}\"%\"\n" +
            "            </if>\n" +
            "            <if test=\"realname !=null and realname !=''\">\n" +
            "                and sui.realname like \"%\"#{realname}\"%\"\n" +
            "            </if>\n" +
            "            <if test=\"id !=null and id !=''\">\n" +
            "                and a.id = #{id}\n" +
            "            </if>\n" +
            "            <if test=\"catalogs !=null and catalogs !=''\">\n" +
            "               and a.catalogs like \"%\"#{catalogs}\"%\"\n" +
            "            </if>\n" +
            "        order by a.stick asc,a.create_time desc" +
            " </script>")
    List<BlogArticleVO> getArticle(@Param("id") String id, @Param("realname") String realname, @Param("title") String title, @Param("catalogs") String catalogs);


    /**
     * 获取文章浏览数
     *
     * @param id
     * @return
     */
    @Select("select view_count from blog_article where id =#{id}")
    String getArticleViewCount(@Param("id") String id);

    /**
     * 获取文章评论
     *
     * @param id
     * @return
     */
    @Select("select count(pk_article) from blog_comment where pk_article =#{id}")
    int getArticleCommentCount(@Param("id") String id);

    /**
     * 浏览数+1
     *
     * @param id
     * @return
     */
    @Update("update blog_article set view_count=#{viewCount} where id =#{id}")
    Integer updateArticleViewCount(@Param("viewCount") int viewCount, @Param("id") String id);

    /**
     * 获取热门文章
     *
     * @return
     */
    @Select("select id,title,icon from blog_article where status = 2  order by view_count desc limit 6")
    List<Map<String, Object>> getHotArticle();

    /**
     * 获取最新文章
     *
     * @return
     */
    @Select("select id,title,icon from blog_article where status = 2 order by create_time desc limit 6")
    List<Map<String, Object>> getNewArticle();

    /**
     * 根据标签进行文章搜索
     *
     * @return
     */
    @Select(" <script>" +
            " select a.*,sui.realname,(select count(*) from blog_comment where  pk_article = a.id) commentCount from blog_article a " +
            "        left join sys_user c on a.pk_user = c.id\n" +
            "        left join sys_user_information sui on a.pk_user = sui.pk_user\n" +
            " where ifnull(a.status,2)= 2 " +
            " <if test=\"className !=null and className !=''\">" +
            " and a.catalogs like concat('%',(SELECT id from blog_classify b WHERE b.classify_name LIKE concat( '%',#{className},'%' ) ),'%')" +
            "</if>" +
            " order by a.stick asc,a.create_time desc" +
            " </script>")
    List<BlogArticleVO> getArticleByClassName(@Param("className") String className);

    /**
     * 获取最新文章
     *
     * @return
     */
    @Select("select * from blog_classify order by create_time desc")
    List<BlogClassifyVO> getClassify();
}
